package com.luck.emp.stockLmt.logic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.springframework.jdbc.core.RowMapper;

import com.luck.emp.entity.EmpTStockLmt;
import com.luck.emp.entity.EmpTWhBalance;
import com.luck.emp.entity.EmpTWhBalanceId;
import com.luck.emp.report.vo.StorageVo;
import com.luck.emp.stockLmt.qry.StockLmtQry;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.dao.BaseJdbcDao;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.Page;

/**
 * 说明 Copyright: Copyright (c) 2011 深圳**科技
 * 
 * @author
 * 
 */
public class StockLmtLogic extends BaseLogic implements IStockLmtLogic {
	private BaseJdbcDao jdbcDao;

	/**
	 * 查找分页信息
	 */
	public Page findStockLmtPage(StockLmtQry qry) {
		Criteria criteria = baseDao.createCriteria(EmpTStockLmt.class);
		CriteriaUtil.addEq(criteria, "id.productCode", qry.getProductCode());
		CriteriaUtil.addEq(criteria, "id.whCode", qry.getWhCode());
		Order order = Order.desc("id.productCode");
		criteria.addOrder(order);
		criteria.createAlias("wh", "vwh");
		criteria.createAlias("vwh.dept", "dept");
		filterData(criteria, qry.getUserInfo(), "dept", null);
		return baseDao.pagedQuery(criteria, qry.getPageNo(), qry.getPageSize());
	}

	/**
	 * 保存
	 **/
	public String saveStockLmt(UserBean userBean, EmpTStockLmt entity)
			throws Exception {
		super.save(entity);
		return null;
	}

	/**
	 * 删除
	 */
	public String deleteStockLmt(UserBean userBean, EmpTStockLmt entity)
			throws Exception {
		super.delete(entity);
		return null;
	}

	public BaseJdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(BaseJdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

	public Page findStockInitPage(StockLmtQry qry) {
		StringBuffer sql = new StringBuffer();
		Map params = new HashMap();
		sql.append("SELECT D.DEPT_NAME       DEPTNAME, ");
				sql.append("       P.V_NAME          PRODUCTNAME, ");
				sql.append("       P.V_PRODUCT_CODE  PRODUCTCODE, ");
				sql.append("       P.V_PROD_BARCODE  PRODBARCODE, ");
				sql.append("       P.V_SKU           SKU, ");
				sql.append("       PARAMS.PARAM_NAME MEASUNIT, ");
				sql.append("       PD.V_NAME         KINDNAME ");
				sql.append("  FROM SYS_DEPARTMENT  D, ");
				sql.append("       EMP_T_PROD_BASE P, ");
				sql.append("       SYS_PARAMS      PARAMS, ");
				sql.append("       EMP_T_PROD_KIND PD ");
				sql.append(" WHERE P.N_DEPT_ID = D.ID ");
				sql.append("   AND PARAMS.PARAM_CODE = 'p_meas_unit' ");
				sql.append("   AND P.V_MEAS_UNIT = PARAMS.PARAM_VALUE ");
				sql.append("   AND P.N_PROD_KIND = PD.N_ID ");
				sql.append("   AND P.V_PRODUCT_CODE NOT IN ");
				sql.append("       (SELECT T.V_PRODUCT_CODE ");
				sql.append("          FROM EMP_T_WH_BALANCE T ");
				
				if(StringUtils.isNotBlank(qry.getWhCode())){
					sql.append(" where t.V_WH_CODE =:whCode ");
					params.put("whCode", qry.getWhCode());
				}
				sql.append("         GROUP BY T.V_WH_CODE, T.V_PRODUCT_CODE) ");
				if(StringUtils.isNotBlank(qry.getProductName()))
					sql.append(" and p.v_name like '%"+qry.getProductName()+"%' ");
				if(StringUtils.isNotBlank(qry.getProductCode())){
					sql.append(" and b.V_PRODUCT_CODE like :productCode ");
					params.put("productCode", "%"+qry.getProductCode()+"%");
				}
				if(qry.getKindId() != null){
					sql.append(" and p.N_PROD_KIND =:kindId ");
					params.put("kindId", qry.getKindId());
				}
				sql.append(" and p.n_dept_id =:deptId ");
				params.put("deptId", qry.getUserInfo().getOrgId());
				//System.out.println(sql.toString());
		return  jdbcDao.queryForPage(sql.toString(), params, qry.getPageNo(),qry.getPageSize(), storageRw);
	}
	private static final StorageRw storageRw = new StorageRw();

	private static final class StorageRw implements RowMapper {
		public Object mapRow(ResultSet rs, int rownum) throws SQLException {
			StorageVo vo = new StorageVo();
			vo.setIndex(rownum + 1);
			vo.setDeptName(rs.getString("deptName"));
			vo.setProdBarcode(rs.getString("PRODBARCODE"));
			vo.setProductCode(rs.getString("productCode"));
			vo.setProductName(rs.getString("productName"));
			vo.setSku(rs.getString("sku"));
			vo.setKindName(rs.getString("kindname"));
			vo.setMeasUnit(rs.getString("MEASUNIT"));
			//vo.setNote(rs.getString("V_NOTE"));
			//vo.setExDId(rs.getLong("N_EX_D_ID"));
			return vo;
		}
	}
	public String saveStockInit(UserBean userBean, String whCode,
			String productCodes) throws Exception {
		String[] prods = productCodes.split(",");
		for(String productCode:prods){
			EmpTWhBalanceId id = new EmpTWhBalanceId();
			EmpTWhBalance balance = new EmpTWhBalance();
			id.setPosiCode("A1F101P1");
			id.setWhCode(whCode);
			id.setProductCode(productCode);
			balance.setId(id);
			balance.setAreaCode("A1");
			balance.setFrameCode("F1");
			balance.setInWh(DateUtil.getDate());
			balance.setLayer(1L);
			balance.setLayerPosiCode("P1");
			balance.setQty(0L);
			balance.setStatus("1");
			balance.setModiDate(DateUtil.getDate());
			save(balance);
		}
		return null;
	}

}